angular.module('app').controller('userCtrl', ['$scope', '$http', 'utils', '$state', 'userInfo', 'product', '$q', function($scope, $http, utils, $state, userInfo, product, $q) {
	/*设置显示隐藏*/
	$scope.eyes = true //隐藏金额
	$scope.phone_id = userInfo.phone //注册界面取手机号
	$scope.seqnumber = null //验证码流水号
	$scope.applyid = null //绑卡申请号
	$scope.UserAssets = {} //用户资产信息
	/*手动初始化input*/
	mui('.mui-input-row input').input();
	/*注册/登录*/
	$scope.checkReg = function() {
		$http.get(utils.userSys, {
			params: {
				header: utils.header("070049"),
				body: {
					"phone_number": $scope.phone
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				userInfo.phone = $scope.phone //更改用户服务手机号
				userInfo.user_code_temp = res.body.data[0].user_code //更改临时用户服务user_code
				console.log(userInfo.user_code_temp)
				userInfo.user_type = res.body.data[0].user_type //更改用户服务信息user_type
				$state.go('login')
			} else if(res.header.re_code == "8000") {

				userInfo.phone = $scope.phone
				userInfo.user_type = 4
				$state.go('register_code')
			}
		})
	}

	/*注册*/
	$scope.regedit = function() {
		/*验证码校验*/
		var checkCode = $http.get(utils.userSys, {
			params: {
				header: utils.header("070053"),
				body: {
					generate_seq: $scope.seqnumber,
					verification_code: $scope.code,
					phone_number: userInfo.phone
				}
			}
		})
		/*注册*/
		checkCode.then(function(res) {
			if(res.header.re_code == "0000") {
				$http.get(utils.userSys, {
					params: {
						header: utils.header("070054"),
						body: {
							download: "100",
							phone_id: "",
							phone_number: userInfo.phone,
							register_ip: "",
							register_time: ""
						}
					}
				}).then(function(res) {
					if(res.header.re_code == "0000") {
						userInfo.user_code_temp = res.body.user_code
						$state.go("SetLoginPassword")
					}
				})

			}
		})
	}

	/*设置登录密码*/
	$scope.SetLoginPassword = function() {
		console.log(userInfo.user_code_temp)
		$http.get(utils.userSys, {
			params: {
				header: utils.header("070055"),
				body: {
					login_type: "0",
					new_password: userInfo.backPwd ? md5($scope.LoginPassword) : "",
					old_password: md5($scope.LoginPassword),
					set_type: userInfo.backPwd ? "1" : "0",
					user_code: userInfo.user_code_temp,
					user_type: userInfo.user_type,
					fingerprint_password: "",
					gesture_password: "",
					third_bind_id: "",
					third_platform_id: ""
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				userInfo.user_code = userInfo.user_code_temp //保存用户
				localStorage.user_code = userInfo.user_code_temp //保存user_code到本地
				localStorage.phone = userInfo.phone //保存手机号到本地
				localStorage.user_type = userInfo.user_type //保存用户类型
				utils.msg("设置成功!", 1, function() {
					userInfo.backPwd = false
					$state.go("home")
				})
			}
		})
	}

	/*登录*/
	$scope.Login = function() {
		$http.get(utils.userSys, {
			params: {
				header: utils.header("070050"),
				body: {
					login_password: md5($scope.LoginPassword),
					login_type: "0",
					phone_number: userInfo.phone,
					user_code: userInfo.user_code_temp,
					user_type: userInfo.user_type
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				userInfo.user_code = userInfo.user_code_temp //保存user_code
				localStorage.user_code = userInfo.user_code //保存user_code到本地
				localStorage.phone = userInfo.phone //保存手机号到本地
				localStorage.user_type = userInfo.user_type //保存用户类型
				$state.go("home")
			}
		})
	}

	/*实名认证*/
	$scope.Realname = function() {
		$http.get(utils.userSys, {
			params: {
				header: utils.header("070056"),
				body: {
					home_address: "",
					sex: "",
					phone_number: userInfo.phone,
					id_number: $scope.FullId,
					id_type: "0",
					phone_id: "",
					real_name: $scope.Fullname,
					birth_day: "",
					marry_status: ""
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				userInfo.user_code = res.body.user_code
				userInfo.user_type = 0
				localStorage.user_code = res.body.user_code
				localStorage.user_type = 0
				$state.go("BindBank")
			}
		})
	}

	/*绑定银行卡*/
	$scope.BindBank = function() {
		$http.get(utils.userSys, {
			params: {
				header: utils.header("070090"),
				body: {
					application_number: $scope.applyid,
					sms_verify: $scope.code,
					user_code: userInfo.user_code
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {

				userInfo.getUserInfo().then(function(res) {

					if(res.is_tra_password == 0) {
						$state.go("SetTradePassword")
					} else {

						utils.msg("设置成功!", 1, function() {
							$state.go("home")
						})

					}
				})

			}
		})

	}
	/*设置交易密码*/
	$scope.SetTradePassword = function() {
		$http.get(utils.userSys, {
			params: {
				header: utils.header("070115"),
				body: {
					new_password: md5($scope.LoginPassword),
					ori_password: "",
					type: userInfo.backPwd ? "2" : "0",
					user_code: userInfo.user_code
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {

				if(!userInfo.backPwd) {
					/*设置交易密码*/
					mui.confirm('绑卡成功，风险测评完成后就可进行投资了。', ' ', ['以后再说', '去测评'], function(e) {
						if(e.index === 0) {
							utils.msg("设置成功!", 1, function() {
								userInfo.backPwd = false
								$state.go("home")
								return
							})
						}
						if(e.index === 1) {
							$state.go("02_Evaluation")
							return
						}

					}, 'div')

				} else {
					/*重置交易密码*/
					utils.msg("设置成功!", 1, function() {
						userInfo.backPwd = false
						$state.go("home")
					})
				}

			}
		})
	}

	/*用户资产*/
	if($state.current.name == "user") {
		$scope.UserAssets.acc_amount = "0.00" //钱包默认余额
		$scope.UserAssets.intrestYesterday = "0.00"
		$scope.UserAssets.assetsCurrentTotal = "0.00"
		/*获取用户资产*/
		$http.post(utils.balanceSys, {}, {
				params: {
					header: utils.header("000012"),
					body: {
						user_code: userInfo.user_code
					}
				}
			})
			.then(function(res) {
				if(res.header.re_code == "0000") {
					angular.extend($scope.UserAssets, res.body)
					product.assetsCurrentTotal = $scope.UserAssets.assetsCurrentTotal
					return $http.get(utils.redSys, {
						params: {
							header: utils.header("180011"),
							body: {
								user_code: userInfo.user_code
							}
						}
					})
				}
			})
			/*获取红包*/
			.then(function(res) {
				if(res.header.re_code == "0000") {
					$scope.redlist = res.body
					return $http.post(utils.balanceSys, {}, {
						params: {
							header: utils.header("000032"),
							body: {
								user_id: userInfo.user_code
							}
						}
					})
				}
			})
			/*获取资产列表*/
			.then(function(res) {
				if(res.header.re_code == "0000") {
					$scope.AssetsList = res.body
					/*获取钱包金额*/
					userInfo.getUserBank().then(function(res) {
						$scope.UserAssets.acc_amount = res.body.card.acc_amount
						userInfo.acc_id = res.body.card.acc_id //存储acc_id
						product.acc_amount = res.body.card.acc_amount
					})
				}
			})

		$scope.getGo = function(name) {
			var promise = userInfo.getUserInfo()
			promise.then(function(res) {
				/*特殊状态*/
				if(res.card_bind_status == 3) {
					$state.go("ValidatePhone")
					return
				}

				/*未实名*/
				if(res.is_real_name == 0) {
					$state.go("Realname")
					return
				}
				/*未绑卡*/
				if(res.is_bind_card == 0) {
					$state.go("BindBank")
					return
				}

				/*未设置交易密码*/
				if(res.is_tra_password == 0) {
					$state.go("SetTradePassword")
					return
				}

				$state.go(name)
			})
		}

		$scope.gotoUrl = function(item) {
			product.assetsInfo = item
			if(item.product_type_pid == 4) {
				$state.go("current") //活期
			} else if(item.product_type_pid == 3) {
				$state.go("regular") //定期
			} else if(item.product_type_pid == 1) {
				$state.go("disperse") //散标
			}

		}

	}

	/*用户红包列表*/
	if($state.current.name == "user_coupon") {
		/*获取加息劵和抵扣金*/
		$http.get(utils.redSys, {
			params: {
				header: utils.header("180011"),
				body: {
					user_code: userInfo.user_code
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$scope.discountList = res.body
			}
		})

		/*初始化slider*/
		var Slider = mui('#productTab').slider()
		$scope.$on('$viewContentLoaded', function(event) {
			setTimeout(function() {
				/*设置滚动区域高度*/
				$('.mui-content').css('height', $(window).height() - 44 + 'px')
				$('.mui-slider-group').css('height', $(window).height() - $('.mui-slider-group').offset().top + 'px');
				$('.mui-scroll').css('min-height', $(window).height() - $('.mui-slider-group').offset().top + 'px')

				mui('.mui-scroll-wrapper').scroll({
					scrollY: true, //是否竖向滚动
					scrollX: false, //是否横向滚动
					startX: 0, //初始化时滚动至x
					startY: 0, //初始化时滚动至y
					indicators: true, //是否显示滚动条
					deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏
					bounce: true //是否启用回弹
				});
			}, 200)

		})

		/*手动触发滑动*/
		$(".product_Tab_Title").find("span").on("click", function() {
			Slider.gotoItem($(this).index())
			$(".product_Tab_Title").find("span").eq($(this).index()).addClass("mui-active").siblings().removeClass("mui-active")
		})
		/*监听事件*/
		$('#productTab').on("slide", function(e) {
			$(".product_Tab_Title").find("span").eq(e.detail.slideNumber).addClass("mui-active").siblings().removeClass("mui-active")
		})

		

		

	}

	/*历史红包*/
	if($state.current.name == "redList_history") {
		$http.get(utils.redSys, {
			params: {
				header: utils.header("180010"),
				body: {
					user_code: userInfo.user_code
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$scope.redlist = res.body
				if($scope.redlist.length > 0) {
					for(var i = 0; i < $scope.redlist.length; i++) {
						if($scope.redlist[i].usage_status != 1 || $scope.redlist[i].usage_status != 2) {
							$scope.redlist.splice(i, 1)
							i--
						}
					}

				}
			}
		})
	}

}])